#include "Lora.h"

uint8_t BufferSize = BUFFER_SIZE;
uint8_t Buffer[BUFFER_SIZE];
uint16_t RxIrqMask = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT;
uint16_t TxIrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT;
AppStates_t AppState = APP_LOWPOWER;
PacketStatus_t packetStatus;
uint32_t frequncy=0;

//上行参数
ModulationParams_t  base_station_modulationParams={
    .PacketType                  = PACKET_TYPE_LORA,
    .Params.LoRa.SpreadingFactor = LORA_SF10,
    .Params.LoRa.Bandwidth       = LORA_BW_1600,
    .Params.LoRa.CodingRate      = LORA_CR_LI_4_7
};

//下行参数
ModulationParams_t  light_modulationParams={
    .PacketType                  = PACKET_TYPE_LORA,
    .Params.LoRa.SpreadingFactor = LORA_SF5,
    .Params.LoRa.Bandwidth       = LORA_BW_0800,
    .Params.LoRa.CodingRate      = LORA_CR_LI_4_7
};

PacketParams_t      packetParams ={
    .PacketType                  = PACKET_TYPE_LORA,
    .Params.LoRa.PreambleLength  = 12,
    .Params.LoRa.HeaderType      = LORA_PACKET_VARIABLE_LENGTH,
    .Params.LoRa.CrcMode         = LORA_CRC_ON,
    .Params.LoRa.InvertIQ        = LORA_IQ_NORMAL
};

RadioCallbacks_t Callbacks =
{
    &OnTxDone,        // txDone
    &OnRxDone,        // rxDone
    NULL,             // syncWordDone
    NULL,             // headerDone
    &OnTxTimeout,     // txTimeout
    &OnRxTimeout,     // rxTimeout
    &OnRxError,       // rxError
    NULL,             // rangingDone
    NULL,             // cadDone
};

bool isMaster = true;

void sx1280_update_freq(uint8_t fre)
{
  frequncy = (2400+fre)*1000000;
}

void sx1280_int(uint8_t fre)
{
    ModulationParams_t modulationParams;
    uint32_t pclk_freq=0;
    frequncy = (2400+fre)*1000000;

    PERIPHERAL_ENABLE(SPI, 0);
    DMAC_DisableSyncRequest(SPI_TX_DMA_REQ(SPI0));
    DMAC_DisableSyncRequest(SPI_RX_DMA_REQ(SPI0));
    pclk_freq = SYS_GetPclkFreq();
    SPI_SclkDivTypeDef spi_sclk_div = SPI_CTRL_SCLK_DIV32;//pclk_freq > 240 ? SPI_CTRL_SCLK_DIV16 : pclk_freq > 120 ? SPI_CTRL_SCLK_DIV8 : SPI_CTRL_SCLK_DIV32;
    SPI_Init(SPI0, spi_sclk_div);

    UTIL_IdleUs(500000);                // 500ms  let DC/DC power ramp up
    Radio.Init( &Callbacks );
    Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
    memset( &Buffer, 0x00, BufferSize );
    SX1280SetInterruptMode( );
    AppState = APP_LOWPOWER;
}

void txparam_to_base_station(uint8_t length)
{ 
    GPIO_SetHigh(RADIO_PA_GPIO_Port,RADIO_PA_Pin);     //发送
    GPIO_SetLow(RADIO_LNA_GPIO_Port, RADIO_LNA_Pin);   //接收
    UTIL_IdleUs(10);
    packetParams.Params.LoRa.PayloadLength       = length;

    Radio.SetStandby( STDBY_RC );
    Radio.SetPacketType( base_station_modulationParams.PacketType );
    Radio.SetModulationParams( &base_station_modulationParams );
    Radio.WriteRegister(0x925,0x32);
    Radio.SetPacketParams( &packetParams );
    Radio.SetRfFrequency( frequncy );
    Radio.SetBufferBaseAddresses( 0x00, 0x00 ); //接收
    Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );
    Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
}

void txparam_to_light(uint8_t length)
{
    GPIO_SetHigh(RADIO_PA_GPIO_Port,RADIO_PA_Pin);     //发送
    GPIO_SetLow(RADIO_LNA_GPIO_Port,RADIO_LNA_Pin);    //接收
    UTIL_IdleUs(10);
    packetParams.Params.LoRa.PayloadLength       = length;
    Radio.SetStandby( STDBY_RC );
    Radio.SetPacketType( light_modulationParams.PacketType );
    Radio.SetModulationParams( &light_modulationParams );
    Radio.WriteRegister(0x925,0x1E);
    Radio.SetPacketParams( &packetParams );
    Radio.SetRfFrequency( frequncy );
    Radio.SetBufferBaseAddresses( 0x00, 0x00 ); 
    Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );
    Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
}

void startRx(void)
{
  GPIO_SetLow(RADIO_PA_GPIO_Port,RADIO_PA_Pin);     //发送
  GPIO_SetHigh(RADIO_LNA_GPIO_Port, RADIO_LNA_Pin); //接收
  packetParams.Params.LoRa.PayloadLength       = BUFFER_SIZE;
  Radio.SetStandby( STDBY_RC );
  Radio.SetPacketType( light_modulationParams.PacketType );
  Radio.SetModulationParams( &light_modulationParams );
  Radio.WriteRegister(0x925,0x1E);
  Radio.SetPacketParams( &packetParams );
  Radio.SetRfFrequency( frequncy );
  Radio.SetBufferBaseAddresses( 0x00, 0x00 ); 
  Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
}
